package com.pg.face.pgface.api;

import com.alibaba.fastjson.JSON;
import com.pg.face.pgface.api.service.FaceBiz;
import com.pg.face.pgface.api.vo.FaceVO;
import com.pg.face.pgface.untils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * FACE相关接口
 *
 */
@Api("FACE接口服务")
@RestController
@RequestMapping("/api/facerest/")

@SuppressWarnings("ALL")
public class FaceRest extends BaseRest {
    private static Logger logger = LoggerFactory.getLogger(FaceRest.class);

    @Value("${config.file.save.path:F:/face/tmp/}")
    private String imageTemPath ;

    @Autowired
    @Qualifier("com.pg.face.pgface.api.service.FaceBiz")
    private FaceBiz faceBiz;

    /**
     * @param reqVO
     * @param request
     * @return
     */
    @PostMapping("checkface/v1")
    @ApiOperation(value = "人脸比对",notes = "两张Base64图片比对相似度")
    /*@ApiImplicitParam(paramType = "header", name = "token", value = "token", required = false)*/
    public ResponseEntity<ResponseMsg<String>> checkface(@Validated @RequestBody FaceVO reqVO, HttpServletRequest request){
        Long start = System.currentTimeMillis();
        logger.info("===checkface=req=1:{}",start);
        ResponseMsg responseMsg = new ResponseMsg<String>();

        float similarity = 0f;

        Long tmpName = System.currentTimeMillis();
        String imgAPath = imageTemPath +"/"+tmpName+"_a.png";
        String imgBPath = imageTemPath+"/"+tmpName+"_b.png";

        boolean genImgA = ImageUtil.generateImage(reqVO.getImg64A(),imgAPath);
        boolean genImgB = ImageUtil.generateImage(reqVO.getImg64B(),imgBPath);
        try {
            if(genImgA && genImgB){
                BufferedImage bufImageA = ImageIO.read(new FileInputStream(imgAPath));
                ImageInfo imageA = ImageUtil.bufferedImage2ImageInfo(bufImageA);

                BufferedImage bufImageB = ImageIO.read(new FileInputStream(imgBPath));
                ImageInfo imageB = ImageUtil.bufferedImage2ImageInfo(bufImageB);
                similarity = faceBiz.getSimilarityByTwoFace(imageA,imageB);
                //设置相似度
                responseMsg.setResponseBody(similarity);
            }else{
                responseMsg.setRetCode(GlobalResponseStatusEnum.FAILURE.getCode());
                responseMsg.setRetDesc("比对失败");
                responseMsg.setResponseBody("比对失败");
            }
        } catch (Exception e) {
            logger.error("========checkface error:{}",e.getMessage(),e);
            responseMsg.setRetCode(GlobalResponseStatusEnum.FAILURE.getCode());
            responseMsg.setRetDesc("比对失败");
            responseMsg.setResponseBody("比对失败");
        }
        logger.info("===checkface=req=end:{}",System.currentTimeMillis()-start);

        ResponseEntity responseEntity = new ResponseEntity<ResponseMsg<String>>(responseMsg, HttpStatus.OK);
        logger.info("==checkface={}", JSON.toJSON(responseEntity));
        return responseEntity;
    }


}
